package com.itheima;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.itheima.mapper.EmpMapper;
import com.itheima.pojo.Emp;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import java.time.LocalDateTime;
import java.util.List;

@SpringBootTest
public class QueryWrapperTest1 {
    @Autowired
    private EmpMapper empMapper;

    @Test
    public void testQueryWrapper() {
        //select id, name, phone, salary from emp where name like '%李%' and salary >= 5000;
        QueryWrapper<Emp> qw = new QueryWrapper<>();
        qw.like("name", "李").ge("salary", 5000).select("id, name, phone, salary");
        List<Emp> emps = empMapper.selectList(qw);
        for (Emp emp : emps) {
            System.out.println(emp);
        }
    }

    @Test
    public void testUpdateWrapper() {
        UpdateWrapper<Emp> uw = new UpdateWrapper<>();
        //update emp set salary = 18000 where name = '李忠'
        uw.set("salary", 18000)
                .eq("name", "李忠");
        int i = empMapper.update(uw);
    }
    @Test
    public void testUpdateWrapper1(){
        UpdateWrapper<Emp> uw = new UpdateWrapper<>();
        //更新id为5, 6, 7的员工的薪水，加2000，代码如下
        //update emp set salary = salary + 2000 where id in (5,6,7)
        uw.setSql("salary = salary + 2000").in("id",5,6,7);
        empMapper.update(uw);
    }
    @Test
    public void testUpdateWrapper2() {
        //UpdateWrapper<Emp> uw = new UpdateWrapper<>();
        Emp emp = new Emp();
        emp.setSalary(18000);
        emp.setUpdateTime(LocalDateTime.now());
        QueryWrapper<Emp> qw = new QueryWrapper<>();
        qw.eq("name","李忠");
        empMapper.update(emp,qw);
    }
    @Test
    public void testLambda() {
        //select id, name, phone, salary from emp where name like '%李%' and salary >= 5000;
        LambdaQueryWrapper<Emp> qw = new LambdaQueryWrapper<>();
        qw.like(Emp::getName, "李").ge(Emp::getSalary, 5000).select(Emp::getId,Emp::getName,Emp::getSalary,Emp::getPhone);
        List<Emp> emps = empMapper.selectList(qw);
        for (Emp emp : emps) {
            System.out.println(emp);
        }
    }

    @Test
    public void testLambda2() {
        //UpdateWrapper<Emp> uw = new UpdateWrapper<>();
        Emp emp = new Emp();
        emp.setSalary(18000);
        emp.setUpdateTime(LocalDateTime.now());
        LambdaQueryWrapper<Emp> qw = new LambdaQueryWrapper<>();
        qw.eq(Emp::getName,"李忠");
        empMapper.update(emp,qw);
    }
    @Test
    public void testLambda3(){
        LambdaUpdateWrapper<Emp> uw = new LambdaUpdateWrapper<>();
        //更新id为5, 6, 7的员工的薪水，加2000，代码如下
        //update emp set salary = salary + 2000 where id in (5,6,7)
        uw.setSql("salary = salary + 2000").in(Emp::getId,5,6,7);
        empMapper.update(uw);
    }
    @Test
    public void testLambda4() {
        LambdaUpdateWrapper<Emp> uw = new LambdaUpdateWrapper<>();
        //update emp set salary = 18000 where name = '李忠'
        uw.set(Emp::getSalary, 18000)
                .eq(Emp::getName, "李忠");
        int i = empMapper.update(uw);
    }
}
